home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / serial / zyxel-1.5-s / zyxel-1 / zplay / dsp.c next >
C/C++ Source or Header  |  1994-08-08  |  3KB  |  153 lines

  1. /* DSP support routines for my programs.
  2.  
  3. */
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <sys/soundcard.h>
  8. #include <sys/ioctl.h>
  9.  
  10. #include "dsp.h"
  11.  
  12.  
  13. /* Audio buffer */
  14. unsigned char * audiobuffer=NULL;
  15. int audioindex=0;
  16.  
  17. /* Set verbose mode */
  18. int verbose=1,abuf_size;
  19.  
  20. /* Allocate buffers */
  21. void dsp_init(int audio) {
  22.  
  23.     ioctl(audio, SNDCTL_DSP_GETBLKSIZE, &abuf_size);
  24.     if (abuf_size < 4096 || abuf_size > 65536) {
  25.         if (abuf_size == -1)
  26.             perror (AUDIO);
  27.         else
  28.             fprintf (stderr, "Invalid audio buffers size %d\n", abuf_size);
  29.         exit (-1);
  30.     }
  31.  
  32.     if(verbose)
  33.         printf("Buffer size: %d\n",abuf_size);
  34.  
  35.     if((audiobuffer=(char*)malloc(abuf_size))==NULL) {
  36.         fprintf(stderr,"Not able to allocate memory\n");
  37.         perror(AUDIO);
  38.         exit(1);
  39.     }
  40.  
  41. }
  42.  
  43.  
  44.  
  45. /* if need a SYNC, 
  46.    (is needed if we plan to change speed, stereo ... during output)
  47. */
  48. void sync_dsp(int audio) {
  49.     if (ioctl (audio, SNDCTL_DSP_SYNC, NULL) < 0) {
  50.         fprintf (stderr, "Unable to sync audio\n");
  51.         perror(AUDIO);
  52.         exit (-1);
  53.     }
  54. }
  55.  
  56. /* setting the speed for output */
  57. void set_dsp_speed (int audio, int dsp_speed) {
  58.     if (ioctl(audio, SNDCTL_DSP_SPEED, &dsp_speed) < 0) {
  59.         fprintf (stderr, "Unable to set audio speed\n");
  60.         perror (AUDIO);
  61.         exit (-1);
  62.     }
  63.     if(verbose)
  64.         printf("Samplerate: %d\n",dsp_speed);
  65.  
  66. }
  67.  
  68. /* Set sampling size */
  69. void set_dsp_samplesize(int audio, int samplesize) {
  70.     int dsp_stereo;
  71.  
  72.     if(ioctl(audio, SNDCTL_DSP_SAMPLESIZE, &samplesize)<0) {
  73.         fprintf(stderr,"Unable to set sample size\n");
  74.         perror(AUDIO);
  75.         exit(-1);
  76.     }
  77.     if(verbose)
  78.         printf("Samplesize: %d\n",samplesize);
  79. }
  80.  
  81.  
  82.  
  83. /* and there are MONO by default */
  84. void set_dsp_mode(int audio, int dsp_stereo ) {
  85.     dsp_stereo = MODE_MONO;
  86.     if(ioctl(audio, SNDCTL_DSP_STEREO, &dsp_stereo)<0) {
  87.         fprintf(stderr,"Unable to set mode\n");
  88.         perror(AUDIO);
  89.         exit(-1);
  90.     }
  91.     if(verbose)
  92.         printf("Mode: %s\n", dsp_stereo==MODE_MONO ? "Mono" : "Stereo");
  93. }
  94.  
  95. /* DSP buffer */
  96.  
  97.  
  98. /* Write a sample to the dsp */
  99. void write_dsp_byte(int audio, int sample) {
  100.     if(audiobuffer==NULL) {
  101.         fprintf(stderr,"Buffer not allocated\n");
  102.         perror(AUDIO);
  103.         exit(1);
  104.     }
  105.  
  106.     /* Write to buffer first, then flush it */
  107.     audiobuffer[audioindex++]=sample;
  108.  
  109.     /* Check if buffer is full, then flush */
  110.     if(audioindex>=abuf_size) {
  111.         if(write(audio,audiobuffer,abuf_size)<0) {
  112.             fprintf(stderr,"Unable to write samples\n");
  113.             perror(AUDIO);
  114.             exit(-1);
  115.         }
  116.         audioindex=0;
  117.     }
  118. }
  119.  
  120. /* Read a sample from the DSP */
  121. void read_dsp_byte(int audio, long * sample) {
  122.  
  123.     if(audiobuffer==NULL) {
  124.         fprintf(stderr,"Buffer not allocated\n");
  125.         perror(AUDIO);
  126.         exit(-1);
  127.     }
  128.  
  129.     if(audioindex>=abuf_size || audioindex==0) {
  130.         if(read(audio,audiobuffer,abuf_size)<0) {
  131.             fprintf(stderr,"Unable to read samples\n");
  132.             perror(AUDIO);
  133.             exit(-1);
  134.         }
  135.         audioindex=0;
  136.     }
  137.     *sample=audiobuffer[audioindex++];
  138. }
  139.  
  140.  
  141. /* Close the dsp */
  142. void close_dsp(int audio) {
  143.     if(write(audio,audiobuffer,audioindex+1)<0) {
  144.         fprintf(stderr,"Unable to write samples\n");
  145.         perror(AUDIO);
  146.         exit(-1);
  147.     }
  148.     audioindex=0;
  149.     audiobuffer=NULL;
  150.     close(audio);
  151. }
  152.  
  153.